import { _decorator, Component, Node, Prefab, director, instantiate, AudioSource } from 'cc';
import WindowManager from '../../core/manager/WindowManager';
import WXTTUtils from '../../core/manager/WXTTUtils';
import AudioMgr from '../../core/utils/AudioMgr';
import Utils from '../../core/utils/Utils';
import { GameNode } from './GameNode';
import { GameUI } from './GameUI';
import { HallUI } from './HallUI';
const { ccclass, property } = _decorator;

@ccclass('Main')
export class Main extends Component {

    static _instance: Main = null;
    public static Instance(): Main {
        return Main._instance;
    }

    _HallUI: HallUI = null;
    _GameUI: GameUI = null;
    _GameNode: GameNode = null;
    start() {
        Main._instance = this;
        WindowManager.ins.register(this.node)
        WindowManager.ins.open("HallUI").then(panle => {
            this._HallUI = panle
        })

        WindowManager.ins.open("GameUI").then(panle => {
            this._GameUI = panle;
            this._GameUI.node.active = false;
        })


        Utils.loadRes("GameNode", "prefabs", Prefab, (err, res) => {
            this._GameNode = (instantiate(res) as Node).getComponent(GameNode);
            director.getScene().addChild(this._GameNode.node);
        })

        const audioSource = this.node.getComponent(AudioSource)!;
      
        // 将节点封装到管理器中
       // audioManager.instance.init(this._audioSource);
       AudioMgr.Instance().audioSource=audioSource
    }

    play() {
        this._HallUI.node.active = false;
        this._GameUI.node.active = true;
        this._GameUI.restart();
        this._GameNode.node.active = true;
        this._GameNode.restart();
    
        WXTTUtils.destroyBanner()
    }

    backHall() {
        
        WXTTUtils.showBanner();
        this._GameNode.node.active = false;
        this._HallUI.node.active = true;
        this._GameUI.node.active = false;
    }
}

